<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工姓名" prop="fullName">
        <el-input style="width: 170px"
          v-model="queryParams.fullName"
          placeholder="请输入员工姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单名称" prop="ordername">
        <el-input style="width: 170px"
          v-model="queryParams.ordername"
          placeholder="请输入订单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>      
      <!-- <el-form-item label="申请时间" prop="applyTimeStart">
        <el-date-picker clearable size="small" style="width: 180px"
          v-model="queryParams.applyTimeStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择申请开始时间">
        </el-date-picker>
      </el-form-item>~
      <el-form-item label="" prop="applyTimeEnd">
        <el-date-picker clearable size="small" style="width: 180px"
          v-model="queryParams.applyTimeEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择申请结束时间">
        </el-date-picker>
      </el-form-item>   -->
      <el-form-item label="归属部门" prop="deptName">
        <el-select
          v-model="queryParams.did"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          placeholder="请选择"
        >
          <el-option
            v-for="item in dnameOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="warning" icon="el-icon-refresh" size="mini" @click="handleExport">导出</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
    </el-row>

    <el-table v-loading="loading" :data="reimbursementList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="员工工号" align="center" prop="accountNumber" />
      <el-table-column label="员工姓名" align="center" prop="fullName" />     
      <!-- <el-table-column label="申请时间" align="center" prop="applyTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="订单名称" align="center" prop="ordername" :show-overflow-tooltip="true" />
      <el-table-column label="归属部门" align="center" prop="dname" />
      <el-table-column label="报销总金额(元)" align="center" prop="rCuntAmount" />
      <el-table-column label="借款总金额(元)" align="center" prop="bmCountAmount" />
      <!-- <el-table-column label="冲款总金额" align="center" prop="womiCountAmount" /> -->
      <el-table-column label="补贴总金额(元)" align="center" prop="siCountAmount" />
      <el-table-column label="工资总金额(元)" align="center" prop="countSalary" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报销管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="流程实例ID" prop="instanceId">
          <el-input v-model="form.instanceId" placeholder="请输入流程实例ID" />
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="员工ID" prop="employeeId">
          <el-input v-model="form.employeeId" placeholder="请输入员工ID" />
        </el-form-item>
        <el-form-item label="总税金(元)" prop="totalamount">
          <el-input v-model="form.totalamount" placeholder="请输入总税金" />
        </el-form-item>
        <el-form-item label="申请人" prop="applyUser">
          <el-input v-model="form.applyUser" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.applyTime"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前处理人" prop="nowApply">
          <el-input v-model="form.nowApply" placeholder="请输入当前处理人" />
        </el-form-item>
        <el-form-item label="当前节点" prop="nowNode">
          <el-input v-model="form.nowNode" placeholder="请输入当前节点" />
        </el-form-item>
        <el-form-item label="总税率" prop="totalrateamount">
          <el-input v-model="form.totalrateamount" placeholder="请输入总税率" />
        </el-form-item>
        <el-form-item label="发票数" prop="count">
          <el-input v-model="form.count" placeholder="请输入发票数" />
        </el-form-item>
        <el-form-item label="审批状态">
          <el-radio-group v-model="form.approveStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReimbursement,listDept, getReimbursement, delReimbursement, addReimbursement, updateReimbursement, exportReimbursement } from "@/api/system/reimbursement";

export default {
  name: "Reimbursement",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //部门类别
      dnameOptions: [],
      // 报销管理表格数据
      reimbursementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        instanceId: null,
        deptId: null,
        employeeId: null,
        totalamount: null,
        createBy: null,
        updateBy: null,
        remark: null,
        applyUser: null,
        applyTime: null,
        applyTimeStart:null,
        applyTimeEnd:null,
        nowApply: null,
        nowNode: null,
        updateTime: null,
        totalrateamount: null,
        count: null,
        approveStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单ID不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门ID不能为空", trigger: "blur" }
        ],
        employeeId: [
          { required: true, message: "员工ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报销管理列表 */
    getList() {
      this.loading = true;
      listReimbursement(this.queryParams).then(response => {
        this.reimbursementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listDept(this.queryParams).then((response) => {
        this.dnameOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reimbursementId: null,
        orderId: null,
        instanceId: null,
        deptId: null,
        employeeId: null,
        totalamount: null,
        createBy: null,
        updateBy: null,
        remark: null,
        applyUser: null,
        applyTime: null,
        nowApply: null,
        nowNode: null,
        updateTime: null,
        totalrateamount: null,
        count: null,
        approveStatus: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.did = ''
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reimbursementId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报销管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reimbursementId = row.reimbursementId || this.ids
      getReimbursement(reimbursementId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报销管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reimbursementId != null) {
            updateReimbursement(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addReimbursement(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reimbursementIds = row.reimbursementId || this.ids;
      this.$confirm('是否确认删除报销管理编号为"' + reimbursementIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReimbursement(reimbursementIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有报销管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportReimbursement(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
